//
//  WHTimeCountDown.m
//  WasteHouse
//
//  Created by snowlu on 2018/4/22.
//  Copyright © 2018年 LittleShrimp. All rights reserved.
//

#import "WHTimeCountDown.h"
#import <UIKit/UIKit.h>
@interface WHTimeCountDown ()
@property (nonatomic, strong) dispatch_source_t timer;

@property (nonatomic, assign) BOOL isEnterBackground;

@end

@implementation WHTimeCountDown

-(instancetype)init{
    if (self = [super init]) {
        [self addNotification];
    }
    return self;
    
}

- (void)addNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:)
                                                 name:UIApplicationWillResignActiveNotification object:nil]; //监听是否触发home键挂起程序.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:)
                                                 name:UIApplicationDidBecomeActiveNotification object:nil];
}
-(void)dealloc{
    [self removeNotification];
}
- (void)removeNotification {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)applicationWillResignActive:(NSNotification *)nofication {
    self.isEnterBackground = YES;
}

- (void)applicationDidBecomeActive:(NSNotification *)nofication {
    self.isEnterBackground = NO;
}
-(void)countDownWithCompleteBlock:(void (^)())completeBlock{
    if (_timer  ==nil) {
        __weak typeof(self) weakSelf = self;
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
        dispatch_source_set_event_handler(_timer, ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                if (!weakSelf.isEnterBackground) {
                    completeBlock();
                }
            });
        });
        dispatch_resume(_timer);
    }
}

-(void)countDownWithTime:(float)time eventBlock:(void (^)())completeBlock{
    if (_timer==nil) {
        __weak typeof(self) weakSelf = self;
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
        dispatch_source_set_timer(_timer,
                                  dispatch_time(DISPATCH_TIME_NOW, time * NSEC_PER_SEC),
                                  time*NSEC_PER_SEC,
                                  0); //每秒执行
        dispatch_source_set_event_handler(_timer, ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                if (!weakSelf.isEnterBackground) {
                    completeBlock();
                }
            });
        });
        dispatch_resume(_timer);
    }
}

-(void)destoryTimer{
    if (_timer) {
        dispatch_source_cancel(_timer);
        _timer = nil;
    }
}
@end
